package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.CityInfo;
import com.atguigu.model.pojo.DistrictInfo;
import com.atguigu.model.pojo.ProvinceInfo;
import com.atguigu.webadmin.service.CityInfoService;
import com.atguigu.webadmin.service.DistrictInfoService;
import com.atguigu.webadmin.service.ProvinceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "地区信息管理")
@RestController
@RequestMapping("/admin/region")
@Slf4j
public class RegionInfoController {

    @Autowired
    private ProvinceInfoService provinceInfoService;

    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private DistrictInfoService districtInfoService;

    @Operation(summary = "查询省份信息列表")
    @GetMapping("province/list")
    public Result<List<ProvinceInfo>> listProvince() {
        List<ProvinceInfo> provinceInfos = provinceInfoService.list();
        log.info("{}",provinceInfos);
        return Result.ok(provinceInfos);
    }

    @Operation(summary = "根据省份id查询城市信息列表")
    @Parameters({
            @Parameter(name = "id",description = "省份id",in = ParameterIn.DEFAULT)
    })
    @GetMapping("city/listByProvinceId")
    public Result<List<CityInfo>> listCityInfoByProvinceId(@RequestParam Long id) {
        //根据省份id进行条件查询列表信息
        LambdaQueryWrapper<CityInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //指定查询条件
        lambdaQueryWrapper.eq(id != null,CityInfo::getProvinceId,id);
        //根据查询条件完成查询所有结果
        List<CityInfo> list = cityInfoService.list(lambdaQueryWrapper);
        log.info("{}",list);
        return Result.ok(list);
    }

    @GetMapping("district/listByCityId")
    @Parameters({
            @Parameter(name = "id",description = "城市id",in = ParameterIn.DEFAULT)
    })
    @Operation(summary = "根据城市id查询区县信息")
    public Result<List<DistrictInfo>> listDistrictInfoByCityId(@RequestParam Long id) {
        //根据城市id查询区县信息
        LambdaQueryWrapper<DistrictInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //指定查询条件
        lambdaQueryWrapper.eq(id != null,DistrictInfo::getCityId,id);
        //执行条件查询
        List<DistrictInfo> list = districtInfoService.list(lambdaQueryWrapper);
        log.info("{}",list);
        return Result.ok(list);
    }

}
